package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ReviewTC_ACT
 */
//@WebServlet(description = "回覆審核測試案例的結果", urlPatterns = { "/ReviewTC_ACT" })
public class ReviewTC_ACT extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReviewTC_ACT() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/** 將此測試項目的 
		 * 	1. isTCReviewed 改為1
		 * **/
		request.setCharacterEncoding("UTF-8");
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		String strSQL = "UPDATE activiti.tms_tpitem SET isTCReviewed=1,isTCAccepted=?,lastUpdatedDate=? WHERE tpid=? AND mrid=?";
		String returnResult = "OK";
		int tpid = Integer.parseInt(request.getParameter("tpid"));
		String mrid = request.getParameter("mrid");
		int isTCAccepted = Integer.parseInt(request.getParameter("isAccepted"));
		int ver = Integer.parseInt(request.getParameter("ver"));
		String creator = request.getParameter("loginUser");
		String comment = request.getParameter("submitorComment").trim();
		
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, isTCAccepted);
			ps.setTimestamp(2, wid.currentTimeStamp());
			ps.setInt(3, tpid);
			ps.setString(4, mrid);
			ps.execute();
			
			/** 將送出的註解儲存起來 **/
			if (!"".equalsIgnoreCase(comment)) {
				wid.keepCommentMR(tpid, mrid, ver, comment, 3, creator, "reviewer", conn, ps);
			}
			
			
			/** 寫入測試計錄 **/
			wid.keeplog(tpid, mrid, ver, 3, creator, conn, ps);

		} catch (SQLException e) {
			returnResult = wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString();
			}
		}
		
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}

}
